﻿using ERP.ERPModels.Production.ProductionManagement;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.ProductionManage;
using ERP.ProductionManage.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using System.Linq;
using System;
using System.Collections.Generic;

namespace ERP.Web.Controllers
{
    public class ProductionDispatchController : Controller
    {
        private readonly IProductionDispatchService _productionDispatchService;

        private readonly IRepository<ProductionDispatch> _productionDispatchRepository;

        public ProductionDispatchController(IProductionDispatchService productionDispatchService, IRepository<ProductionDispatch> productionDispatchRepository)
        {
            _productionDispatchService = productionDispatchService;
            _productionDispatchRepository = productionDispatchRepository;
        }

        public IActionResult ProductionDispatchIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取生产派单列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ProductionDispatchList([FromQuery] ProductionDispatchPageListDTO param)
        {
            // 1. 构建查询
            var query = await _productionDispatchRepository
                .GetQueryableAsync();

            // 2. 应用筛选条件
            if (!string.IsNullOrEmpty(param.OrderNumber))
            {
                query = query.Where(x => x.OrderNumber.Contains(param.OrderNumber));
            }
            
            if (!string.IsNullOrEmpty(param.TaskName))
            {
                query = query.Where(x => x.TaskName.Contains(param.TaskName));
            }
            
            if (!string.IsNullOrEmpty(param.WorkstationName))
            {
                query = query.Where(x => x.WorkstationName.Contains(param.WorkstationName));
            }

            if (!string.IsNullOrEmpty(param.GradingMatch))
            {
                query = query.Where(x => x.GradingMatch.Contains(param.GradingMatch));
            }

            // 3. 计算总记录数
            var totalCount = await query.CountAsync();

            // 4. 执行分页查询
            var items = await query
                .OrderBy(x => x.OrderNumber)
                .Skip((param.PageIndex - 1) * param.PageSize)
                .Take(param.PageSize)
                .ToListAsync();

            // 5. 构建返回结果
            return Json(new
            {
                code = 0,
                msg = "success",
                count = totalCount,
                data = items
            });
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateProductionDispatch([FromBody]ProductionDispatchUpdateDTO param)
        {
            var result = await _productionDispatchService.UpdateProductionDispatch(param);
            return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteProductionDispatch(int id)
        {
            await _productionDispatchService.DeleteProductionDispatch(id);
            return Json(new { success = true, message = "删除成功" });
        }
    }
}
